﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Architecture.Data;
using HF.Framework;
using HF.Permission.Data;
using HF.WorkFlow.Const;

public partial class Resources_ArchitectureView : System.Web.UI.Page
{
    /// <summary>
    /// 递归装载全部组织结构
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    private  void LoadArchitecture(string key, TreeNodeCollection startNodes)
    {
        try
        {
            if (key == null || key == "") key = "0";
            DataTable table = ArchitectureData.GetInstance().GetChildArchitectures(key);
            foreach (DataRow row in table.Rows)
            {

                TreeNode tmpNode = new TreeNode();
                tmpNode.Value = row["ArchitectureId"].ToString();
                tmpNode.Text = row["Caption"].ToString();
                if (row["TypeName"].ToString() == WorkFlowConst.Architecture_Department)
                {
                    tmpNode.ImageUrl = @"image\folder.bmp";

                }
                else
                    if (row["TypeName"].ToString() == WorkFlowConst.Architecture_Duty)
                    {
                        tmpNode.ImageUrl = @"image\duty.bmp";
                    }
                loadArchUser(tmpNode,tmpNode.Value);
                startNodes.Add(tmpNode);
                LoadArchitecture(tmpNode.Value, tmpNode.ChildNodes);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    /// <summary>
    /// 加载用户
    /// </summary>
    /// <param name="startNode"></param>
    /// <param name="archId"></param>
    private void loadArchUser(TreeNode startNode,string archId)
    {
        DataTable dtArch = ArchitectureData.GetInstance().GetUsers(archId);
        foreach (DataRow dr in dtArch.Rows)
        {
            TreeNode tmpNode = new TreeNode();
            tmpNode.Value = dr["UserId"].ToString();
            tmpNode.ImageUrl = @"image\mytask.bmp";
            tmpNode.Text = dr["UserName"].ToString() + "[" + dr["UserId"].ToString() + "]";
            startNode.ChildNodes.Add(tmpNode);
        }
    }
    private void loadGroup(TreeNodeCollection startNodes)
    {
        DataTable dtGroup =GroupData.GetInstance().GetAllGroups();
        foreach (DataRow dr in dtGroup.Rows)
        {
            TreeNode tmpNode = new TreeNode();
            tmpNode.Value = dr["GroupId"].ToString();
            tmpNode.ImageUrl = @"image\folder.bmp";
            tmpNode.Text = dr["GroupName"].ToString() ;
            loadGroupUser(tmpNode,tmpNode.Value);
            startNodes.Add(tmpNode);
        }
    }
    private void loadGroupUser(TreeNode startNode, string groupId)
    {
        DataTable dtGroup = GroupData.GetInstance().GetUsersOfGroup(groupId);
        foreach (DataRow dr in dtGroup.Rows)
        {
            TreeNode tmpNode = new TreeNode();
            tmpNode.Value = dr["UserId"].ToString();
            tmpNode.ImageUrl = @"image\mytask.bmp";
            tmpNode.Text = dr["UserName"].ToString() + "(" + dr["UserId"].ToString() + ")";
            startNode.ChildNodes.Add(tmpNode);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadArchitecture(WorkFlowConst.Architecture_Department, tvArch.Nodes);
            tvArch.ExpandAll();

            loadGroup(tvGroup.Nodes);
            tvGroup.ExpandAll();
        }
    }
}
